//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "CommodityCashFlow.h"
using namespace Cephei::QL::Experimental::Commodities;
#include <gen/QL/Money.h>
#include <gen/QL/Currency.h>
#include <gen/QL/CashFlow.h>
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Commodities::CCommodityCashFlow::CCommodityCashFlow (DateTime date, Cephei::QL::IMoney^ discountedAmount, Cephei::QL::IMoney^ undiscountedAmount, Cephei::QL::IMoney^ discountedPaymentAmount, Cephei::QL::IMoney^ undiscountedPaymentAmount, Double discountFactor, Double paymentDiscountFactor, Boolean finalized) : CCashFlow(CCommodityCashFlow::typeid)
{
    CMoney^ _CdiscountedAmount;
    CMoney^ _CundiscountedAmount;
    CMoney^ _CdiscountedPaymentAmount;
    CMoney^ _CundiscountedPaymentAmount;
    try
    {
#ifdef HANDLE
        _phCommodityCashFlow = NULL;
#endif
        QuantLib::Date _date = (QuantLib::Date)ValueHelper::Convert (date);
        _CdiscountedAmount = safe_cast<CMoney^> (discountedAmount);
        _CdiscountedAmount->Lock();
        QuantLib::Money _discountedAmount = static_cast<QuantLib::Money> (_CdiscountedAmount->GetReference ()); 
        _CundiscountedAmount = safe_cast<CMoney^> (undiscountedAmount);
        _CundiscountedAmount->Lock();
        QuantLib::Money _undiscountedAmount = static_cast<QuantLib::Money> (_CundiscountedAmount->GetReference ()); 
        _CdiscountedPaymentAmount = safe_cast<CMoney^> (discountedPaymentAmount);
        _CdiscountedPaymentAmount->Lock();
        QuantLib::Money _discountedPaymentAmount = static_cast<QuantLib::Money> (_CdiscountedPaymentAmount->GetReference ()); 
        _CundiscountedPaymentAmount = safe_cast<CMoney^> (undiscountedPaymentAmount);
        _CundiscountedPaymentAmount->Lock();
        QuantLib::Money _undiscountedPaymentAmount = static_cast<QuantLib::Money> (_CundiscountedPaymentAmount->GetReference ()); 
        QuantLib::Real _discountFactor = (QuantLib::Real)ValueHelper::Convert (discountFactor);
        QuantLib::Real _paymentDiscountFactor = (QuantLib::Real)ValueHelper::Convert (paymentDiscountFactor);
        bool _finalized = (bool)ValueHelper::Convert (finalized);
        _ppCommodityCashFlow = new boost::shared_ptr<QuantLib::CommodityCashFlow> (new QuantLib::CommodityCashFlow ( _date,  _discountedAmount,  _undiscountedAmount,  _discountedPaymentAmount,  _undiscountedPaymentAmount,  _discountFactor,  _paymentDiscountFactor,  _finalized ));
        SetCashFlow (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppCommodityCashFlow));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CdiscountedAmount != nullptr) _CdiscountedAmount->Unlock();
        if (_CundiscountedAmount != nullptr) _CundiscountedAmount->Unlock();
        if (_CdiscountedPaymentAmount != nullptr) _CdiscountedPaymentAmount->Unlock();
        if (_CundiscountedPaymentAmount != nullptr) _CundiscountedPaymentAmount->Unlock();
    }
}
Cephei::QL::Experimental::Commodities::CCommodityCashFlow::CCommodityCashFlow (boost::shared_ptr<QuantLib::CommodityCashFlow>& childNative, Object^ owner) : CCashFlow(CCommodityCashFlow::typeid)
{
#ifdef HANDLE
	_phCommodityCashFlow = NULL;
#endif
	_ppCommodityCashFlow = &childNative;
    _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppCommodityCashFlow));
}
Cephei::QL::Experimental::Commodities::CCommodityCashFlow::CCommodityCashFlow (QuantLib::CommodityCashFlow& childNative, Object^ owner) : CCashFlow(CCommodityCashFlow::typeid)
{
#ifdef HANDLE
	_phCommodityCashFlow = NULL;
#endif
	_ppCommodityCashFlow = new boost::shared_ptr<QuantLib::CommodityCashFlow> (&childNative);
    _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppCommodityCashFlow));
    _CommodityCashFlowOwner = owner;
    _CashFlowOwner = owner;
}

Cephei::QL::Experimental::Commodities::CCommodityCashFlow::CCommodityCashFlow (CCommodityCashFlow^ copy) : CCashFlow(CCommodityCashFlow::typeid)
{
#ifdef HANDLE
	_phCommodityCashFlow = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCommodityCashFlow = new boost::shared_ptr<QuantLib::CommodityCashFlow> (copy->GetShared());
        _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppCommodityCashFlow));
    }
}
Cephei::QL::Experimental::Commodities::CCommodityCashFlow::CCommodityCashFlow (System::Type^ t) : CCashFlow(CCommodityCashFlow::typeid)
{
#ifdef HANDLE
	_phCommodityCashFlow = NULL;
#endif
	if (!t->IsSubclassOf(CCommodityCashFlow::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Commodities::CCommodityCashFlow::CCommodityCashFlow (QuantLib::Handle<QuantLib::CommodityCashFlow>& childNative, Object^ owner)  : CCashFlow(CCommodityCashFlow::typeid)
{
	_phCommodityCashFlow = &childNative;
	_ppCommodityCashFlow = &static_cast<boost::shared_ptr<QuantLib::CommodityCashFlow>>(childNative.currentLink());
    _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppCommodityCashFlow));
    _CommodityCashFlowOwner = owner;
}
Cephei::QL::Experimental::Commodities::CCommodityCashFlow::CCommodityCashFlow (QuantLib::Handle<QuantLib::CommodityCashFlow> childNative)  : CCashFlow(CCommodityCashFlow::typeid)
{
	_phCommodityCashFlow = &childNative;
	_ppCommodityCashFlow = &static_cast<boost::shared_ptr<QuantLib::CommodityCashFlow>>(childNative.currentLink());
    _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppCommodityCashFlow));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Commodities::CCommodityCashFlow::CCommodityCashFlow (QuantLib::CommodityCashFlow childNative)  : CCashFlow(CCommodityCashFlow::typeid)
{
#ifdef HANDLE
	_phCommodityCashFlow = NULL;
#endif
	_ppCommodityCashFlow = new boost::shared_ptr<QuantLib::CommodityCashFlow> (new QuantLib::CommodityCashFlow (childNative));
    _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppCommodityCashFlow));
}
#endif

Cephei::QL::Experimental::Commodities::CCommodityCashFlow::~CCommodityCashFlow ()
{
    if (_ppCommodityCashFlow != NULL)
    {
	    delete _ppCommodityCashFlow;
        _ppCommodityCashFlow = NULL;
    }
}
Cephei::QL::Experimental::Commodities::CCommodityCashFlow::!CCommodityCashFlow ()
{
    if (_ppCommodityCashFlow != NULL)
    {
	    delete _ppCommodityCashFlow;
    }
}
QuantLib::CommodityCashFlow& Cephei::QL::Experimental::Commodities::CCommodityCashFlow::GetReference ()
{
    if (_ppCommodityCashFlow == NULL) throw gcnew NativeNullException ();
	return **_ppCommodityCashFlow;
}
boost::shared_ptr<QuantLib::CommodityCashFlow>& Cephei::QL::Experimental::Commodities::CCommodityCashFlow::GetShared ()
{
    if (_ppCommodityCashFlow == NULL) throw gcnew NativeNullException ();
	return *_ppCommodityCashFlow;
}
QuantLib::CommodityCashFlow* Cephei::QL::Experimental::Commodities::CCommodityCashFlow::GetPointer ()
{
    if (_ppCommodityCashFlow == NULL) throw gcnew NativeNullException ();
	return &**_ppCommodityCashFlow;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CommodityCashFlow>& Cephei::QL::Experimental::Commodities::CCommodityCashFlow::GetHandle ()
{
	if (_phCommodityCashFlow == NULL)
	{
		_phCommodityCashFlow = new Handle<QuantLib::CommodityCashFlow> (*_ppCommodityCashFlow);
	}
	return *_phCommodityCashFlow;
}
#endif
bool Cephei::QL::Experimental::Commodities::CCommodityCashFlow::HasNative () 
{
	return (_ppCommodityCashFlow != NULL);
}

Double Cephei::QL::Experimental::Commodities::CCommodityCashFlow::Amount::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCommodityCashFlow)->amount ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::ICurrency^ Cephei::QL::Experimental::Commodities::CCommodityCashFlow::Currency::get ()
{
    try
    {
    	QuantLib::Currency& _rv = (QuantLib::Currency&)(*_ppCommodityCashFlow)->currency ( );   
        Cephei::QL::CCurrency^ _nrv = gcnew Cephei::QL::CCurrency (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Experimental::Commodities::CCommodityCashFlow::Date::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppCommodityCashFlow)->date ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IMoney^ Cephei::QL::Experimental::Commodities::CCommodityCashFlow::DiscountedAmount::get ()
{
    try
    {
    	QuantLib::Money& _rv = (QuantLib::Money&)(*_ppCommodityCashFlow)->discountedAmount ( );   
        Cephei::QL::CMoney^ _nrv = gcnew Cephei::QL::CMoney (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IMoney^ Cephei::QL::Experimental::Commodities::CCommodityCashFlow::DiscountedPaymentAmount::get ()
{
    try
    {
    	QuantLib::Money& _rv = (QuantLib::Money&)(*_ppCommodityCashFlow)->discountedPaymentAmount ( );   
        Cephei::QL::CMoney^ _nrv = gcnew Cephei::QL::CMoney (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Commodities::CCommodityCashFlow::DiscountFactor::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCommodityCashFlow)->discountFactor ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Experimental::Commodities::CCommodityCashFlow::Finalized::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppCommodityCashFlow)->finalized ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Commodities::CCommodityCashFlow::PaymentDiscountFactor::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCommodityCashFlow)->paymentDiscountFactor ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IMoney^ Cephei::QL::Experimental::Commodities::CCommodityCashFlow::UndiscountedAmount::get ()
{
    try
    {
    	QuantLib::Money& _rv = (QuantLib::Money&)(*_ppCommodityCashFlow)->undiscountedAmount ( );   
        Cephei::QL::CMoney^ _nrv = gcnew Cephei::QL::CMoney (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IMoney^ Cephei::QL::Experimental::Commodities::CCommodityCashFlow::UndiscountedPaymentAmount::get ()
{
    try
    {
    	QuantLib::Money& _rv = (QuantLib::Money&)(*_ppCommodityCashFlow)->undiscountedPaymentAmount ( );   
        Cephei::QL::CMoney^ _nrv = gcnew Cephei::QL::CMoney (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Commodities::ICommodityCashFlow^ Cephei::QL::Experimental::Commodities::CCommodityCashFlow_Factory::Create (DateTime date, Cephei::QL::IMoney^ discountedAmount, Cephei::QL::IMoney^ undiscountedAmount, Cephei::QL::IMoney^ discountedPaymentAmount, Cephei::QL::IMoney^ undiscountedPaymentAmount, Double discountFactor, Double paymentDiscountFactor, Boolean finalized)
{
    return gcnew CCommodityCashFlow ( date,  discountedAmount,  undiscountedAmount,  discountedPaymentAmount,  undiscountedPaymentAmount,  discountFactor,  paymentDiscountFactor,  finalized);
}
